ImageGear Java PDF
Add and Delete Pages

This topic provides information about the following:

If no document is loaded or if a document does not have any pages, then you cannot call most methods on it. For example, you cannot save a document that does not have any pages.

Adding Pages to a PDF Document

  1. Use the createDocument method of the PDF class to create a new instance of the Document class.
  2. Use the insertBlankPage method of the Document class to add a new page to the PDF document. You can provide the desired width and height of the page to create, otherwise the default Letter page (8.5 x 11.0 inches) will be created.
    • Note that page numbers are zero-based, so the first page number is 0.
    • To insert the page at the beginning of the document, provide 0 as the pageNumber argument value.
    • To insert the new page at the end of the document, provide the document page count as the pageNumber argument value. Use the getPageCount method to get the number of pages in the PDF document.
    • To insert the new page before a specific page of the document, provide the value of that page number as the pageNumber argument value; the newly added page will have the page number you provided.

The following is an illustration of how to add blank pages into a previously loaded or a new PDF document:

 
Copy Code
import com.accusoft.imagegearpdf.*;
 
class PdfDemo
{
       private PDF pdf;
       private Document document;
 
       // Add some new blank pages to a PDF document.
       public void addPages()
       {
              // Insert a new blank page at the beginning of the document.
              document.insertBlankPage(0);
 
             // Insert a new blank page at the beginning of the document,
             // the previously inserted page will become the second.
             document.insertBlankPage(0);
 
             // Insert a new blank page at the end of the document.
             document.insertBlankPage(document.getpageCount());
 
             // Insert a new blank page to be at second position in the document.
             document.insertBlankPage(1);
       }
}

Deleting Pages from a PDF Document

  1. Use the createDocument method of the PDF class to create a new instance of the Document class.
  2. Use the openDocument method to load a PDF file.
  3. Use the deletePage method of the Document class to delete a specific page from the PDF document.
    • Note that page numbers are zero-based, so the first page number is 0.
    • Note that after deleting a page, the page numbers for any subsequent pages are all shifted down by one page. Programmers should be careful to delete multiple pages starting with the highest page number to the lowest page number.
    • To delete the first page of the document, provide 0 as the pageNumber argument value.
    • To delete the last page of the document, provide the document page count minus 1 as the pageNumber argument value. Use getPageCount method to get the number of pages in the PDF document.

The following is an illustration of how to delete all pages of a previously loaded PDF document:

 
Copy Code
import com.accusoft.imagegearpdf.*;
 
class PdfDemo
{
       private PDF pdf;
       private Document document;
 
       // Delete all pages from the PDF document.
       public void deleteAllPages()
       {
              // Get number of pages in the PDF document.
              long count = document.getPageCount();
 
              // Delete all the pages of the document.
              for (long pageNumber = count - 1;  pageNumber >= 0; --pageNumber)
              {
                    	document.deletePage(pageNumber);
              }
       }
}

See Also

 

 


©2016. Accusoft Corporation. All Rights Reserved.

Send Feedback